import { storeToRefs } from 'pinia'
import { useAccountStore } from '@/store/account'
import { useAppStore } from '@/store/app'
import { TAB_NAMES as HOME_TAB_NAMES, TAB_TYPE as HOME_TAB_TYPE } from '@/constant/home'

/** 如果進入的頁面需要登入狀態 */
export default (to, from, next) => {
  const accountStore = useAccountStore()
  const { isLogin } = storeToRefs(accountStore)
  const { afterLoginAction } = accountStore
  const { isMounted } = storeToRefs(useAppStore())

  if (to.meta.checkLogin && !isLogin.value) {
    if (isMounted.value) {
      afterLoginAction(next)(to)
    } else {
      next({ name: 'home', params: { tab: HOME_TAB_NAMES[HOME_TAB_TYPE.FORYOU] } })
    }
  } else {
    next()
  }
}
